package com._520it.wms.service.impl;

import com._520it.wms.domain.Department;
import com._520it.wms.mapper.DepartmentMapper;
import com._520it.wms.query.PageResult;
import com._520it.wms.query.QueryObject;
import com._520it.wms.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dd on 2017/11/15.
 */
@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public void delete(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(Department department) {
        if (department.getId() != null){
            departmentMapper.updateByPrimaryKey(department);
        }else{
            departmentMapper.insert(department);
        }
    }

    @Override
    public Department get(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> listAll() {
        return departmentMapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject qo) {
        int count = departmentMapper.queryForCount(qo);
        if(count==0){
            return new PageResult(qo.getCurrentPage(),qo.getPageSize(),new ArrayList<Object>(),0);
        }else{
            List<Department> list = departmentMapper.queryForList(qo);
            return new PageResult(qo.getCurrentPage(),qo.getPageSize(),list,count);
        }
    }
}
